package com.yangzb.http.apache;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.thread.ThreadUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class Test {
    public static final String URL = "https://api.github.com/users/lianjiatech";
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService = ThreadUtil.newExecutor(10, 10, 100000);
        List<Call> tasks = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Call thread = new Call(i);
            tasks.add(thread);
        }
        StopWatch stopWatch = StopWatch.create("call");
        stopWatch.start();
        List<Future<Integer>> futures = executorService.invokeAll(tasks);
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());
        System.out.println(stopWatch.getTotalTimeSeconds());

        for (Future<Integer> future : futures) {
            Integer integer = future.get();
        }
        executorService.shutdown();
    }

    public static class Call implements Callable<Integer>{
        private Integer i ;

        public Call(Integer i){
            this.i = i;
        }
        @Override
        public Integer call() throws Exception {
            HttpClientUtil.sendGetRequest(URL+"?t="+i,null);
            return i;
        }
    }
}
